<?php

namespace app\admin\dict;

use think\facade\Cache;

class IconDict
{
    const ICON_KEY = '__icon__';
    public static function updateIconDict(array $data)
    {
        $fileName = root_path().'public/assets/icon/iconfont.json';
        $json = json_encode($data);
        file_put_contents($fileName, $json);
        Cache::set(self::ICON_KEY, $data);
    }
    public static function getIconDict($refresh=false,$callback=null)
    {
        $cache = Cache::get(self::ICON_KEY);
        if(empty($cache)||$refresh){
            $str  = file_get_contents(root_path().'public/assets/icon/iconfont.json');
            $str = json_decode($str, true);
            $cache = "";
            foreach ($str as $key => $value){
                if($callback){
                    $cache .= $callback($value);
                }else{
                    $cache .= '<div style="margin:15px;cursor: pointer" ><el-icon :size="size" @click="baocun(`'.$value.'`)"><'.$value.' /></el-icon></div>';
                }
            }
            Cache::set(self::ICON_KEY, $cache);
        }
        return $cache;
    }
    public static function getIconDictCallBack($callback=null)
    {
        $str  = file_get_contents(root_path().'public/assets/icon/iconfont.json');
        $str = json_decode($str, true);
        $cache = "";
        foreach ($str as $key => $value){
            if($callback){
                $cache .= $callback($value);
            }else{
                $cache .= '<div style="margin:15px" ><el-icon :size="size" @click="baocun(`'.$value.'`)"><'.$value.' /></el-icon></div>';
            }
        }
        return $cache;
    }
}